package Array;

import org.junit.Test;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

/**
 * @Classname 算术三元组的数目2367
 * @Description TODO
 * @Date 2023/3/31 7:00
 * @Created by xjl
 */
public class 算术三元组的数目2367 {

    /**
     * @description 采用暴力的解析
      * @param: nums
     * @param: diff
     * @date: 2023/3/31 7:37
     * @return: int
     * @author: xjl
    */
    public int arithmeticTriplets0(int[] nums, int diff) {
        int ans = 0;
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                if (nums[j] - nums[i] != diff) {
                    continue;
                }
                for (int k = j + 1; k < n; k++) {
                    if (nums[k] - nums[j] == diff) {
                        ans++;
                    }
                }
            }
        }
        return ans;
    }

    /**
     * @description 采用的Hashmap的数据结构来实现
      * @param: nums
     * @param: diff
     * @date: 2023/3/31 7:20
     * @return: int
     * @author: xjl
    */
    public int arithmeticTriplets(int[] nums, int diff) {
        int result = 0;
        ArrayList<Integer> list = new ArrayList<>();
        for (int i : nums) {
            list.add(i);
        }

        for (int i : nums) {
            int k = 1;
            while (k < 3) {
                if (list.contains(i + diff)) {
                    k++;
                    i += diff;
                } else {
                    break;
                }
            }
            if (k == 3) {
                result++;
            }
        }
        return result;
    }

    /**
     * @description 这样的一种的是更为简单就是保证两个树都在list里面
      * @param: nums
     * @param: diff
     * @date: 2023/3/31 7:22
     * @return: int
     * @author: xjl
    */
    public int arithmeticTriplets2(int[] nums, int diff) {
        Set<Integer> set = new HashSet<Integer>();
        for (int x : nums) {
            set.add(x);
        }
        int ans = 0;
        for (int x : nums) {
            if (set.contains(x + diff) && set.contains(x + 2 * diff)) {
                ans++;
            }
        }
        return ans;
    }

    /**
     * @description 采用的是三指针的方式来实现 减少了的存的空间 直接使用的数组来判断下标的是否在数组中，如果在那就是存储，如果是不存在，那就是不存在
      * @param: nums
     * @param: diff
     * @date: 2023/3/31 7:27
     * @return: int
     * @author: xjl
    */
    public int arithmeticTriplets3(int[] nums, int diff) {
        int ans = 0;
        int n = nums.length;
        for (int i = 0, j = 1, k = 2; i < n - 2 && j < n - 1 && k < n; i++) {
            j = Math.max(j, i + 1);
            while (j < n - 1 && nums[j] - nums[i] < diff) {
                j++;
            }
            if (j >= n - 1 || nums[j] - nums[i] > diff) {
                continue;
            }
            k = Math.max(k, j + 1);
            while (k < n && nums[k] - nums[j] < diff) {
                k++;
            }
            if (k < n && nums[k] - nums[j] == diff) {
                ans++;
            }
        }
        return ans;
    }

    @Test
    public void test() {
        int i = arithmeticTriplets(new int[]{0, 1, 4, 6, 7, 10}, 3);
    }
}
